package logic

import (
	"context"
	"xialou_platform/app/constx"
	"xialou_platform/app/errorx"
	"xialou_platform/app/requestx"
	"xialou_platform/app/respx"
	"xialou_platform/services/system/rpc/systemClient"
	"xialou_platform/services/wallet/rpc/walletClient"

	"xialou_platform/services/wallet/api/internal/svc"
	"xialou_platform/services/wallet/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type WalletWithdrawLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewWalletWithdrawLogic(ctx context.Context, svcCtx *svc.ServiceContext) WalletWithdrawLogic {
	return WalletWithdrawLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *WalletWithdrawLogic) WalletWithdraw(req *types.WalletWithdrawReq) error {
	logx.Infof("提现参数：%+v", req)
	if req.Amount < 100 {
		return errorx.NewDefaultError("提现金额不能低于1元哦。")
	}
	uid, err := requestx.FetchUserIdByJwt(l.ctx)
	if err != nil {
		return err
	}
	var sourceId int64
	switch req.Platform {
	case "xiaoke":
		sourceId = uid
	case "xiuxian":
		sourceId = respx.IDParse(req.BusinessId).GetInt64()

	}

	Robot, err := l.svcCtx.SystemRpc.SysConfigByKey(l.ctx, &systemClient.KeyReq{Key: constx.WithdrawRobotUrl})
	if err != nil {
		return err
	}

	_, err = l.svcCtx.WalletRpc.WalletWithdraw(l.ctx, &walletClient.WalletWithdrawReq{
		SourceId: sourceId,
		Amount:   req.Amount,
		Robot:    Robot.ConfigValue,
	})
	if err != nil {
		return err
	}

	return nil
}
